package org.ch00.test08;

import java.io.File;
import java.io.IOException;

/*
 *
 *@author XCH
 *@date 2025 - 03 - 07 - 15:55
 *
 */
public class FileCopyThread extends Thread {
    private String sourcePath;
    private String destinationPath;
    private long totalBytes;
    private volatile long copiedBytes;  // 使用 volatile 确保可见性
    private FileCopyExample fileCopyExample;

    public FileCopyThread(String sourcePath, String destinationPath) {
        this.sourcePath = sourcePath;
        this.destinationPath = destinationPath;
        this.fileCopyExample = new FileCopyExample();
        this.totalBytes = new File(sourcePath).length();
        this.copiedBytes = 0;
    }

    @Override
    public void run() {
        try {
            fileCopyExample.copyFile(sourcePath, destinationPath, this);

            System.out.println("文件拷贝完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public long getCopiedBytes() {
        return copiedBytes;
    }

    public long getTotalBytes() {
        return totalBytes;
    }

    public void updateCopiedBytes(long bytes) {
        this.copiedBytes += bytes;
    }
}